package com.etc.service;

import java.util.ArrayList;

import com.etc.dao.RoleDAO;
import com.etc.entity.Career;
import com.etc.entity.Role;
import com.etc.entity.RoleEquipment;

public class RoleService {

	RoleDAO roleDao = new RoleDAO();
	CareerService careerService = new CareerService();
	RoleEquipmentService reService = new RoleEquipmentService();
	
	//添加一个角色
	public Role addRole(String nickName, Career career, RoleEquipment re, String userId) {
		Role role = attrIntegration(nickName ,career, re, userId);
		roleDao.addRole(role);
		return role;
	}
	//根据id获取一个角色
	public Role findRoleById(String roleId) {
		Role role = roleDao.findRoleById(roleId);
		return role;
	}
	
	//属性整合
	public Role attrIntegration(String nickName, Career career, RoleEquipment re, String userId) {
		//获取初始属性
		int heal = career.getStartHeal();
		int image = career.getStartImage();
		int attack = career.getStartAttack();
		int critical = career.getStartCrit();
		int defense = career.getStartDefense();
		int speed = career.getStartSpeed();
		//初始属性加上装备属性
		heal += re.getMainWeap().getAttrHeal() 
				+ re.getHelmet().getAttrHeal() + re.getTop().getAttrHeal() 
				+ re.getBottom().getAttrHeal() + re.getShoe().getAttrHeal();
		image += re.getMainWeap().getAttrImage() 
				+ re.getHelmet().getAttrImage() + re.getTop().getAttrImage() 
				+ re.getBottom().getAttrImage() + re.getShoe().getAttrImage();
		attack += re.getMainWeap().getAttrAttack()
				+ re.getHelmet().getAttrAttack() + re.getTop().getAttrAttack() 
				+ re.getBottom().getAttrAttack() + re.getShoe().getAttrAttack();
		critical += re.getMainWeap().getAttrCrit()
				+ re.getHelmet().getAttrCrit() + re.getTop().getAttrCrit() 
				+ re.getBottom().getAttrCrit() + re.getShoe().getAttrCrit();
		defense += re.getMainWeap().getAttrDefense() 
				+ re.getHelmet().getAttrDefense() + re.getTop().getAttrDefense() 
				+ re.getBottom().getAttrDefense() + re.getShoe().getAttrDefense();
		speed += re.getMainWeap().getAttrSpeed()  
				+ re.getHelmet().getAttrSpeed() + re.getTop().getAttrSpeed() 
				+ re.getBottom().getAttrSpeed() + re.getShoe().getAttrSpeed();
		Role role = new Role(nickName, heal, image, attack, critical, defense, speed, re, career, userId);
		System.out.println("heal:"+heal+" image:"+image+" attack:"
					+attack+" critical:"+critical+" defense:"+defense+" speed:"+speed);
		return role;
	}
	
	public ArrayList<Role> findAllRoleByUserId(String userId) {
		return roleDao.findAllRoleByUserId(userId);
	}
	
	//测试
	public static void main(String[] args) {
		RoleService roleSer = new RoleService();
	//	roleSer.addRole();
		//System.out.println(roleSer.findRoleById("ceaf6100"));
		ArrayList<Role> roles = roleSer.findAllRoleByUserId("12345678");
		for (Role role : roles) {
			System.out.println(role.toString());
		}
		
	}
}
